package cn.csust.net2.manager.client.app;

import cn.csust.net2.manager.client.resources.Resources;
import cn.csust.net2.manager.client.util.ClassTools;
import cn.csust.net2.manager.shared.service.LoginService;
import cn.csust.net2.manager.shared.service.LoginServiceAsync;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.util.Padding;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.custom.ThemeSelector;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout.VBoxLayoutAlign;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Image;

/**
 * UI最上部分的封装
 * 
 * @author Administrator
 * 
 */
public class HeadPanel extends ContentPanel implements AppPanel {
	protected App app;
	private final Image image = new Image();
	//用户头像的地址
	private String imageURL = App.getUser().getUserHead();
	public Image getImage() {
		return image;
	}

	public HeadPanel(App app) {
		this.app = app;
		init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		this.setHeaderVisible(false);
		createToolBar();
	}

	@Override
	public BorderLayoutData getLayoutInfo() {
		BorderLayoutData layoutData = new BorderLayoutData(LayoutRegion.NORTH,
				92);
		layoutData.setMargins(new Margins(0, 0, 5, 0));
		return layoutData;
	}

	/**
	 * 创建工具栏，工具栏上是常用操作的快捷方式
	 */
	public void createToolBar() {
		ToolBar toolbar = new ToolBar();

		Button exitButton = new Button();
		exitButton.setSize(85, 85);
		exitButton
				.setIcon(AbstractImagePrototype.create(Resources.ICONS.exit()));
		exitButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				MessageBox.confirm("提示", "你确定要退出系统吗",
						new Listener<MessageBoxEvent>() {

							@Override
							public void handleEvent(MessageBoxEvent be) {
								Button btn = be.getButtonClicked();
								if (btn.getItemId().equals("yes")) {
									LoginServiceAsync service = GWT
											.create(LoginService.class);
									service.LoginOut(new AsyncCallback<Void>() {

										@Override
										public void onSuccess(Void result) {
											App.refrash();
										}

										@Override
										public void onFailure(Throwable caught) {

										}
									});
								}
							}
						});
			}
		});

		//首页
		Button homeButton = new Button();
		homeButton.setSize(85, 85);
		homeButton.setIcon(AbstractImagePrototype.create(Resources.ICONS.home()));
		
		//个人 信息
		Button personalInfo = createToolbarButton(Resources.ICONS.personalinfo(), "学生基本信息", "StudentInformation");
		
		//年级查看
		Button grade = createToolbarButton(Resources.ICONS.grade(), "年级查看", "ByGrade");
		
		//公告
		Button notice = createToolbarButton(Resources.ICONS.notice(), "查看公告", "Notice");
		
		//投票
		Button vote = createToolbarButton(Resources.ICONS.vote(), "投票", "Vote");
		
		//审核
		Button verify = createToolbarButton(Resources.ICONS.verify(), "信息审核", "CheckStudentInfo");
		
		//在线交流
		Button talkOnline = createToolbarButton(Resources.ICONS.talkOnline(), "在线交流", "TalkOnline");
		

		//主题选择器，可以更换主题
		ThemeSelector selector = new ThemeSelector();
		selector.setWidth(80);
		
		toolbar.add(createUserPanel());
		toolbar.add(homeButton);
		toolbar.add(personalInfo);
		toolbar.add(grade);
		toolbar.add(notice);
		toolbar.add(vote);
		toolbar.add(verify);
		toolbar.add(talkOnline);
		toolbar.add(exitButton);
		toolbar.add(selector);
		setTopComponent(toolbar);
	}
	
	/**
	 * 根据图片、显示的名称、改按钮所对应的权限的名称创建一个按钮，
	 * 是工具栏上该按钮所对应模块的快捷方式
	 * @param icon
	 * @param displayName
	 * @param authorityName
	 * @return
	 */
	public Button createToolbarButton(ImageResource icon, final String displayName, final String authorityName) {
		Button button = new Button();
		button.setSize(85, 85);
		button.setIcon(AbstractImagePrototype.create(icon));
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				//TabSetPanel panel = app.getTabSetPanel();
				if(app.getTabSetPanel().getItemByItemId(displayName) == null){
					TabItem tabItem = ClassTools.newInstance(TabSetPanel.BASE_CLASS_URL+authorityName);
					app.getTabSetPanel().add(tabItem);
				}
				app.getTabSetPanel().setSelection(app.getTabSetPanel().getItemByItemId(displayName));
			}
		});
		return button;
	}
	
	/**
	 * 创建用户信息面板，该面板用来显示用户头像、用户名等当前登录用户的基本信息
	 * @return
	 */
	public ContentPanel createUserPanel() {
		ContentPanel userPanel = new ContentPanel();
		userPanel.setSize(200, 80);
		userPanel.setHeaderVisible(false);
		userPanel.setFrame(true);
		userPanel.setLayout(new ColumnLayout());
		
		ContentPanel userHead = new ContentPanel();
		userHead.setHeaderVisible(false);
		userHead.setSize(64, 64);
		
		if(imageURL==null || imageURL.equals("")) {
			image.setUrl("images/userHead/default_person.gif");
		} else {
			image.setUrl(GWT.getModuleBaseURL() + "download_center.do?type=P");
		}
		image.setSize("64px", "64px");
		image.setVisible(true);
		userHead.add(image);

		LayoutContainer right = new LayoutContainer();
		right.setSize(60, 80);
		VBoxLayout layout = new VBoxLayout();  
	    layout.setPadding(new Padding(10));  
	    layout.setVBoxLayoutAlign(VBoxLayoutAlign.STRETCH);  
		right.setLayout(layout);
		right.add(new LabelField(App.getUser().getName()));
//		right.add(new LabelField(App.getUser().get));
		
		userPanel.add(userHead, new ColumnData(.5));		
		userPanel.add(right, new ColumnData(.5));
		
		return userPanel;
	}
}
